package com.my.content.talent;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.my.content.bean.TalentBean;
import com.my.content.liveproject.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 李雪斌 on 2016/12/21.
 */
public class TalentListAdapter extends BaseAdapter{
    private final Context context;
    private List<TalentBean.LivesBean> data;
    private final LayoutInflater inflater;

    public TalentListAdapter(FragmentActivity activity) {
        context = activity;
        inflater = LayoutInflater.from(context);
    }

    //设置数据
    public void setData(List<TalentBean.LivesBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return data != null ? data.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.hot_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //绑定数据
        TalentBean.LivesBean livesBean = data.get(position);
        holder.hotListItemNick.setText(livesBean.getCreator().getNick());
        holder.hotListItemAddress.setText(livesBean.getCity());
        holder.hotListItemWatches.setText(livesBean.getOnline_users()+"");
        holder.hotListItemSaying.setText(livesBean.getName());
        String portrait = livesBean.getCreator().getPortrait();
        //设置图片
//        Picasso.with(context).load(portrait).placeholder(R.mipmap.default_room).into(holder.hotListItemIcon);
//        Picasso.with(context).load(portrait).placeholder(R.mipmap.default_room).into(holder.hotListItemPersonImage);
        //设置图片
        Glide.with(context).load(portrait).placeholder(R.mipmap.default_room).into(holder.hotListItemIcon);
        Glide.with(context).load(portrait).placeholder(R.mipmap.default_room).into(holder.hotListItemPersonImage);
        return convertView;
    }


    static class ViewHolder {
        @BindView(R.id.hot_list_item_icon)
        ImageView hotListItemIcon;
        @BindView(R.id.hot_list_item_nick)
        TextView hotListItemNick;
        @BindView(R.id.hot_list_item_address)
        TextView hotListItemAddress;
        @BindView(R.id.hot_list_item_watches)
        TextView hotListItemWatches;
        @BindView(R.id.hot_list_item_person_image)
        ImageView hotListItemPersonImage;
        @BindView(R.id.hot_list_item_saying)
        TextView hotListItemSaying;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
